<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/9/22
 * Time: 12:27
 * 定时任务
 */
namespace app\api\controller;

use think\Controller;
use think\Db;

class Task extends Controller {
	
	/**
	 * 取消2小时内的不付款订单
	 */
	public function cancelOrder() {
		$time = time() - 7200;
		$orders = Db::name('order')
			->where(['create_time' => ['elt', $time], 'status' => \app\common\model\Order::STATUS_WAIT])
			->field('id, order_no')
			->select();
		
		$str = '================='.date('Y-m-d　H:i:s').'======================'.PHP_EOL;
		if($orders) {
			$model = new \app\common\model\Order();
			foreach ($orders as $order) {
				$res = $model->cancel($order['id'], $model::CANCEL_REASON_TIME);
				if (!$res) {
					$str .= $model->getError() . PHP_EOL;
				}
			}
			$str .= '取消成功'.count($orders).'单';
		} else {
			$str .= '没有超时订单'.PHP_EOL;
		}
		
		echo $str;
	}
	
	
	/**
	 * 签收7天前的订单
	 */
	public function signOrder() {
		$time = strtotime('-7 days');
		$orders = Db::name('order')
			->where(['shipping_time' => ['elt', $time], 'status' => \app\common\model\Order::STATUS_SHIPPING])
			->field('id, order_no')
			->select();
		
		$str = '================='.date('Y-m-d　H:i:s').'======================'.PHP_EOL;
		if($orders) {
			$model = new \app\common\model\Order();
			foreach ($orders as $order) {
				$res = $model->sign($order['id']);
				if (!$res) {
					$str .= $model->getError() . PHP_EOL;
				}
			}
			$str .= '签收成功'.count($orders).'单';
		} else {
			$str .= '没有待签收订单'.PHP_EOL;
		}
		
		echo $str;
	}
}
